package sgi.client;

import java.io.IOException;
import java.util.Iterator;

import javax.swing.JOptionPane;

import sgi.details_new.AccountInfoDetails;
import sgi.details_new.ImageDetails;
import sgi.details_new.ImageFullDetails;
import sgi.message.MessageGetImageEdit;
import sgi.message.MessageImageAdd;
import sgi.message.MessageImageEdit;
import sgi.message.MessageReviewImages;
import sgi.message.MessageViewInfo;

public class SGIImageEditController extends SGIController {

	public static SGIImageEditController editReview;
	public static SGIImageEditUI editReviewUI;
	/** constructor */
	public SGIImageEditController() {
		SGIImageEditController.editReviewUI = new SGIImageEditUI();
	}


	/** create controller */
	public static SGIImageEditController create() throws IOException
	{
		if(SGIImageEditController.editReview == null)
		{
			SGIImageEditController.editReview = new SGIImageEditController();
			editReviewUI.setImageEditController(SGIImageEditController.editReview);
		}
		SGIImageEditController.editReviewUI.setVisible(true);
		return editReview;
	}
	
	/** terminate UI  */
	public static void terminate()
	{
		SGIImageEditController.editReviewUI.setVisible(false);
		SGIImageEditController.editReview=null;
		SGIImageEditController.editReviewUI.setVisible(false);
		SGIImageEditController.editReviewUI=null;
	}
	/**sending message of viewing the images by imageid */
	public void EditReview() {
		MessageReviewImages msg = new MessageReviewImages();
		sendEditReview(msg);
		
	}
	/**sending to clientconnection  */
	private void sendEditReview(MessageReviewImages msg) {
		try {
			(SGIClientConnectionController.createConnection()).handleMessageFromClientUI(msg);
		} catch  (IOException e) {e.printStackTrace();}
	}
	/**sending message of getting the fields of the selected imageid */
	public void MessageFromImageEditUI(int imageid) {
		MessageGetImageEdit msg = new MessageGetImageEdit(imageid);
		try {
				SGIClientConnectionController conn = SGIClientConnectionController.createConnection();
				conn.handleMessageFromClientUI(msg);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	/**  sending message of updating the column of image selected */
	public void update(int imageid,String Date,String Location, String Latitude, 
			String Longitude,byte[] originalImage1, byte[] originalImage2,
			byte[] originalImage3, byte[] originalImage4, byte[] originalImage5)
	{
		MessageImageEdit msg = new MessageImageEdit(imageid,Date,Location,Latitude,Longitude,originalImage1,
				originalImage2,originalImage3, originalImage4, originalImage5);
		try {
			SGIClientConnectionController.createConnection().handleMessageFromClientUI(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/** handle message getting imageid */
	@Override
	void handleMessage(Object msg) {
		Iterator<ImageDetails> iterator = (((MessageReviewImages)msg).getimageDetails()).iterator();
		SGIImageEditController.editReviewUI.clearTable();
		while (iterator.hasNext()) {
			SGIImageEditController.editReviewUI.addRows(iterator.next());
		}
	}
	/** if field not inserted for updating */
	public void FieldNotInserted()
	{
		JOptionPane.showMessageDialog(SGIImageEditController.getUI(),"not all fields filled");
	}
	/** handle message of notifying */
	void handleMessageEdit(Object msg) {
		editReviewUI.notify("Images have been updated.");

	}
	/** handle message of getting all fields of selected imageid from image table */
	void handleMessageImageFullDetails(Object msg)
	{
		Iterator<ImageFullDetails> iterator = (((MessageGetImageEdit)msg).getimageDetails()).iterator();
		SGIImageEditController.editReviewUI.addImageInfo(iterator.next());
	}
	/** getUI  */
	public static SGIImageEditUI getUI()
	{
		return editReviewUI;
	}

}
